package kjx_test_security.auth.entity;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @Author 康1919
 * @Date 2024/7/20 14:12
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class KcAuthUserModel implements UserDetails {
    private Long id;
    private String username;
    private String password;
    private List<Long> roleIds;
    private List<String> permissions;
    private  boolean isAccountNonExpired=true;
    private  boolean isAccountNonLocked=true;
    private  boolean isCredentialsNonExpired=true;
    private  boolean isEnabled=true;


    //存储SpringSecurity所需要的权限信息的集合
    // 封装用户权限的放回集合，避免每次登录都要创建其对象
    // @JSONField(serialize = false) 不参与用户序列化 GrantedAuthority类型是不能被序列化的
    @JSONField(serialize = false)
    private List<GrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        if (authorities != null){
            return authorities;
        }
        authorities = permissions
                .stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
        return authorities;
    }





    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
